package com.zzty.txj.util;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.zzty.txj.entity.TestMessage;
import org.slf4j.LoggerFactory;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by 2YSP on 2017/7/12.
 */
public class MessageUtil {
    private static final org.slf4j.Logger logger = LoggerFactory.getLogger(MessageUtil.class);

    public static final String URL = "http://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN";
    public void sendMessage(HttpServletRequest request) throws Exception {
        //获取token
        String token = WeiChatUtil.getAccessToken();
        String url = URL.replace("ACCESS_TOKEN", token);
        HttpSession session=request.getSession();
        String openID=(String)session.getAttribute("openId");
        System.out.println("openID"+openID);
        TestMessage massMessage = new TestMessage();
        List<String> list = new ArrayList<String>();
        list.add(openID);
        list.add(openID);
        massMessage.setTouser(list);
        massMessage.setMsgtype("text");
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("content",  "欢迎来到编码训练营,"+"<a href=\'" + "http://www.aboutit.com.cn/html/user.html" + "\'>" + "点击我，立即绑定" + "</a>");
        massMessage.setText(map);
        logger.info("return message"+map);
        Gson gson = new GsonBuilder().setPrettyPrinting().disableHtmlEscaping().create();
        String str = gson.toJson(massMessage);
        System.out.println("str:"+str);
        String jso = HttpUtil.httpPostWithJSON(url, str);
        System.out.println(jso.toString());
    }
}